﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using Zpovednice;

namespace GrammarNazi
{
    class Program
    {
        static void Main(string[] args)
        {
            ZpovedniceAPI api = new ZpovedniceAPI();

            var loginResult = api.Login("Melodie Logiky", "test");

            if (loginResult == LoginResult.Error)
                throw new Exception("Login error");
            
            int lastId = 0;

            while (true)
            {
                //var confessions = api.GetConfessionList();

                var confessions = new List<Confession>();
                confessions.Add(new Confession {Id = 549897});
                foreach (var confessionId in confessions.Select(c=>c.Id).Where(id => id > lastId))
                {
                    var confession = api.GetConfession(confessionId);
                    foreach (var absolution in confession.Absolutions)
                    {
                        if (absolution.Creator.IsAnonymous)
                            continue;

                        var errors = CheckText(absolution.Text);

                        if (errors.Count > 0)
                        {
                            StringBuilder sb = new StringBuilder();
                            sb.AppendFormat("Váš příspěvek ve zpovědi {0} obsahoval chybu.", confession.Id);
                            sb.AppendLine();
                            foreach (var error in errors)
                            {
                                sb.Append(error.Description);
                                sb.AppendLine();
                            }

                            api.SendPersonalMessage(absolution.Creator.Id.Value, sb.ToString());

                        }
                    }

                    lastId = Math.Max(lastId, confessionId);
                }
                Thread.Sleep(1000);
            }
            
        }

        private static List<GrammaticalError> CheckText(string text)
        {
            List<GrammaticalError> errors = new List<GrammaticalError>();

            foreach (var rule in Ruleset)
            {
                errors.AddRange(rule.Check(text));
            }
         
            return errors;
        }

        public static List<IGrammaticalRule> Ruleset = new List<IGrammaticalRule>
                                                           {
                                                               new VyjenovanaSlova(),
                                                            };

        public interface IGrammaticalRule
        {
            IEnumerable<GrammaticalError> Check(string text);
        }

        public class VyjenovanaSlova : IGrammaticalRule
        {
            public List<string> PossibleErrors = new List<string>()
                                                     {
                                                         "netopír",
                                                     };

            public IEnumerable<GrammaticalError> Check(string text)
            {
                foreach (string word in GetWords(text))
                {
                    if (PossibleErrors.Contains(word.ToLower()))
                    {
                        yield return new GrammaticalError {Description = string.Format("Slovo {0} je výjmenované slovo", word)};
                    }
                }
            }
        }

        public static IEnumerable<string> GetWords(string text)
        {
            foreach (Match match in Regex.Matches(text, @"[\S]+"))
            {
                string word = match.Value;
                word = word.Replace("(", "").Replace(")", "");

                yield return word;
            }
        }
    }
}
